/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import entity.Client;
import entity.IndicativeProductPrice;
import entity.SalesInquiry;
import entity.SalesInquiryItem;
import java.sql.Timestamp;
import java.util.Collection;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import session.IndicativeProductPriceManager;
import session.SalesInquiryManager;

/**
 *
 * @author Samuel
 */
@ManagedBean
@SessionScoped
public class SalesInquiryBean {

    @EJB
    private SalesInquiryManager salesInquiryManager;
    @EJB
    private IndicativeProductPriceManager ippm;
    
    private Collection<Client> clients; //= new ArrayList<Client>();;
    private Long clientId;
    private String businessType;
    private Timestamp dateTime;
    private Collection<IndicativeProductPrice> productsList;
    private Long indicativeProductPriceId;
    private String productName;
    private Double productPrice;
    private Integer numOfProduct;
    private Collection<SalesInquiry> salesInquiryList;
    private Collection<SalesInquiryItem> salesInquiryItems;
    private Long salesInquiryId;
    private Double totalPrice;
    private SalesInquiry salesInquiry;
    private String status;

    
    /** Creates a new instance of SalesInquiryBean */
    public SalesInquiryBean() {
        totalPrice = 0.0;
    }

    public void retrieveClients(ActionEvent event) {
        clients = salesInquiryManager.getClients();

    }

    public void addNewSalesInquiry(ActionEvent event) {
        Client c = new Client();
        c = salesInquiryManager.getClient(clientId);
        businessType = c.getBusinessType();


        salesInquiryManager.createSalesInquiry();

        dateTime = salesInquiryManager.getSalesInquiry().getDateTime();

        retrieveProductsList(businessType);
    }

    public void clientAskForSalesInquiry(ActionEvent event) {

        Client c = new Client();

        //COMMENT OFF UNTIL INTEGRATION, UNTESTED CODE
        //String username  = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");   
        // c = quotationManager.getClient(username);
        // clientId = c.getId();

        //to be removed/replaced by part above during INTEGRATION , need to have a client in database with id=43 for this to work
        c = salesInquiryManager.getClient(43L);
        clientId = c.getId();


        this.setBusinessType(c.getBusinessType());
        salesInquiryManager.createSalesInquiry();

        dateTime = (salesInquiryManager.getSalesInquiry().getDateTime());
        retrieveProductsList(businessType);


    }

    public void addNewSalesInquiryItem(ActionEvent event) {

        for (Object o : productsList) {
            IndicativeProductPrice temp = (IndicativeProductPrice) o;
            if (temp.getId().equals(indicativeProductPriceId)) {
                productName = temp.getProductName();
                productPrice = temp.getPrice();
            }
        }
        salesInquiryManager.addSalesInquiryItem(productName, numOfProduct, productPrice);
        totalPrice += productPrice * numOfProduct;
    }

    public void confirmNewSalesInquiry(ActionEvent event) {
        // quotationManager.persistQuotation();


        salesInquiryManager.getSalesInquiry().setClient(salesInquiryManager.getClient(clientId));

        salesInquiryManager.getSalesInquiry().setStatus("Completed");
        salesInquiryManager.persistSalesInquiry();
        salesInquiryId = salesInquiryManager.getSalesInquiry().getId();

    }

    public void resetItem(ActionEvent event) {
        productName = null;
        productPrice = null;
        numOfProduct = null;
    }

    public void getSalesInquiryItems(ActionEvent event) {
        salesInquiryItems = salesInquiryManager.getSalesInquiryItems();
    }

    public void resetAfterConfirmSalesInquiry(ActionEvent event) {

        resetAll();

    }

    public void viewSalesInquiry(ActionEvent event) {
        salesInquiry = salesInquiryManager.getSalesInquiry(salesInquiryId);

    }

    public void resetAll() {

        productName = null;
        numOfProduct = null;
        productPrice = null;
        dateTime = null;
        salesInquiryItems = null;
        salesInquiryList = null;
        totalPrice = 0.0;
        productsList = null;
        businessType = null;
        clients = null;
        clientId = null;
        indicativeProductPriceId = null;
        status = null;

    }

    public void retrieveProductsList(String businessType) {
        productsList = ippm.getProductsList(businessType);
    }

    public void getSalesInquiryList(ActionEvent event) {
        salesInquiryList = salesInquiryManager.getSalesInquiryList();
    }

    public void getClientSalesInquiryList(ActionEvent event) {

        Client c = new Client();

        //COMMENT OFF UNTIL INTEGRATION, UNTESTED CODE
        //String username  = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");   
        // c = quotationManager.getClient(username);

        //to be removed/replaced by part above during INTEGRATION , need to have a client in database with id=43 for this to work
        c = salesInquiryManager.getClient(43L);


        clientId = c.getId();
        salesInquiryList = salesInquiryManager.getSalesInquiryList(clientId);
    }
    
    public void prepareEditSalesInquiry(ActionEvent event)
    {
        status = salesInquiryManager.getSalesInquiry(salesInquiryId).getStatus();
    }
    
    public void editSalesInquiry(ActionEvent event){
        salesInquiryManager.editSalesInquiry(salesInquiryId, status);
       
    }
    /**
     * @return the salesInquiryManager
     */
    public SalesInquiryManager getSalesInquiryManager() {
        return salesInquiryManager;
    }

    /**
     * @param salesInquiryManager the salesInquiryManager to set
     */
    public void setSalesInquiryManager(SalesInquiryManager salesInquiryManager) {
        this.salesInquiryManager = salesInquiryManager;
    }

    /**
     * @return the clients
     */
    public Collection<Client> getClients() {
        return clients;
    }

    /**
     * @param clients the clients to set
     */
    public void setClients(Collection<Client> clients) {
        this.clients = clients;
    }

    /**
     * @return the clientId
     */
    public Long getClientId() {
        return clientId;
    }

    /**
     * @param clientId the clientId to set
     */
    public void setClientId(Long clientId) {
        this.clientId = clientId;
    }

    /**
     * @return the ippm
     */
    public IndicativeProductPriceManager getIppm() {
        return ippm;
    }

    /**
     * @param ippm the ippm to set
     */
    public void setIppm(IndicativeProductPriceManager ippm) {
        this.ippm = ippm;
    }

    /**
     * @return the businessType
     */
    public String getBusinessType() {
        return businessType;
    }

    /**
     * @param businessType the businessType to set
     */
    public void setBusinessType(String businessType) {
        this.businessType = businessType;
    }

    /**
     * @return the dateTime
     */
    public Timestamp getDateTime() {
        return dateTime;
    }

    /**
     * @param dateTime the dateTime to set
     */
    public void setDateTime(Timestamp dateTime) {
        this.dateTime = dateTime;
    }

    /**
     * @return the productsList
     */
    public Collection<IndicativeProductPrice> getProductsList() {
        return productsList;
    }

    /**
     * @param productsList the productsList to set
     */
    public void setProductsList(Collection<IndicativeProductPrice> productsList) {
        this.productsList = productsList;
    }

    /**
     * @return the indicativeProductPriceId
     */
    public Long getIndicativeProductPriceId() {
        return indicativeProductPriceId;
    }

    /**
     * @param indicativeProductPriceId the indicativeProductPriceId to set
     */
    public void setIndicativeProductPriceId(Long indicativeProductPriceId) {
        this.indicativeProductPriceId = indicativeProductPriceId;
    }

    /**
     * @return the productName
     */
    public String getProductName() {
        return productName;
    }

    /**
     * @param productName the productName to set
     */
    public void setProductName(String productName) {
        this.productName = productName;
    }

    /**
     * @return the productPrice
     */
    public Double getProductPrice() {
        return productPrice;
    }

    /**
     * @param productPrice the productPrice to set
     */
    public void setProductPrice(Double productPrice) {
        this.productPrice = productPrice;
    }

    /**
     * @return the numOfProduct
     */
    public Integer getNumOfProduct() {
        return numOfProduct;
    }

    /**
     * @param numOfProduct the numOfProduct to set
     */
    public void setNumOfProduct(Integer numOfProduct) {
        this.numOfProduct = numOfProduct;
    }

    /**
     * @return the salesInquiryItems
     */
    public Collection<SalesInquiryItem> getSalesInquiryItems() {
        return salesInquiryItems;
    }

    /**
     * @param salesInquiryItems the salesInquiryItems to set
     */
    public void setSalesInquiryItems(Collection<SalesInquiryItem> salesInquiryItems) {
        this.salesInquiryItems = salesInquiryItems;
    }

    /**
     * @return the salesInquiryId
     */
    public Long getSalesInquiryId() {
        return salesInquiryId;
    }

    /**
     * @param salesInquiryId the salesInquiryId to set
     */
    public void setSalesInquiryId(Long salesInquiryId) {
        this.salesInquiryId = salesInquiryId;
    }

    /**
     * @return the salesInquiryList
     */
    public Collection<SalesInquiry> getSalesInquiryList() {
        return salesInquiryList;
    }

    /**
     * @param salesInquiryList the salesInquiryList to set
     */
    public void setSalesInquiryList(Collection<SalesInquiry> salesInquiryList) {
        this.salesInquiryList = salesInquiryList;
    }

    /**
     * @return the totalPrice
     */
    public Double getTotalPrice() {
        return totalPrice;
    }

    /**
     * @param totalPrice the totalPrice to set
     */
    public void setTotalPrice(Double totalPrice) {
        this.totalPrice = totalPrice;
    }

    /**
     * @return the salesInquiry
     */
    public SalesInquiry getSalesInquiry() {
        return salesInquiry;
    }

    /**
     * @param salesInquiry the salesInquiry to set
     */
    public void setSalesInquiry(SalesInquiry salesInquiry) {
        this.salesInquiry = salesInquiry;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }
}
